package com.origin.baselibrary.fragment

import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.origin.baselibrary.viewmodel.BaseViewModel
import com.origin.utils.log.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import java.lang.reflect.ParameterizedType

abstract class ViewModelFragment<VB : ViewDataBinding, VM : BaseViewModel>(layoutId: Int) : BaseFragment<VB>(layoutId), CoroutineScope by MainScope() {


    val viewModel: VM by lazy {
        LogUtils.__e("初始化===> ${mFragmentTitle}")
        ViewModelProvider(requireActivity()).get(getTClass())
    }

    private fun getTClass(): Class<VM> {
        return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
    }

    override fun pageLogical() {
        mBinding.lifecycleOwner = this
    }

}